#!/usr/bin/python
#***************************************************************************
# Copyright notice for this file:
#  Copyright (C) 2012 TheAxeMan
#  _______ _                               _    _
# |__   __| |     ___     /\          ___ | \  / |
#    | |  | |__  / _ \   /  \  __  _ / _ \|  \/  | __ _ _ __
#    | |  | '_ \|  __/  / /\ \ \ \/ |  __/| .  . |/ _` | '_ \
#    | |  | | | | (__/\/ ____ \ )  (| (__/| |\/| | (_| | | | |
#    |_|  |_| |_|\____/_/    \_._/\_.\____|_|  |_|\__,_|_| |_|
#
# Final Fantasy TAS scripts by TheAxeMan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#***************************************************************************

"""
attributes:
  dir_counter - counter in sequence of directions
  rand_table_counter - counter in random-number table
  battle_type_counter - counter in list of 1-8 battle types
"""
class GameState:
  direction_sequence = [-1, -1, 1, -1, 1, 1, -1, 1]

  def copy(self):
    """Returns a copy of current state. Fails if state hasn't been powerCycled."""
    gstate_copy = GameState()
    gstate_copy.dir_counter = self.dir_counter
    gstate_copy.rand_table_counter = self.rand_table_counter
    gstate_copy.battle_type_counter = self.battle_type_counter
    return gstate_copy

  def powerCycle(self):
    """sets up counters on power cyle"""
    self.dir_counter = 0
    self.rand_table_counter = 255
    self.battle_type_counter = 0

  def getAndUpdateTypeCounter(self):
    """returns current battle type counter then increments it"""
    ret = self.battle_type_counter
    self.battle_type_counter += 1
    if self.battle_type_counter > 255:
      self.battle_type_counter = 0
    return ret

  def takeStep(self):
    """take one step and return counter value"""
    #update table counter, wrapping if necessary
    self.rand_table_counter += self.direction_sequence[self.dir_counter]
    #print self.rand_table_counter
    if self.rand_table_counter < 0:
      self.rand_table_counter = 255
    elif self.rand_table_counter > 255:
      self.rand_table_counter = 0

    #if new table counter is 0, update dir_counter
    if self.rand_table_counter == 0:
      self.dir_counter += 1
      if self.dir_counter > 7: self.dir_counter = 0

  def takeSteps(self, num):
    """given a number of steps, returns a list of counter values
    that will be traversed"""
    counter_list = []
    for i in range(num):
      self.takeStep()
      counter_list.append(self.rand_table_counter)
    return counter_list

  def printState(self):
    print "dir_counter",self.dir_counter,"rand_counter",self.rand_table_counter,"btype_counter",self.battle_type_counter

def main():
  gstate = GameState()
  gstate.powerCycle()
  print gstate.takeSteps(1000)

if __name__ == "__main__":
  main()

